const { storeListUrl, stockListUrl } = require("../../config")
const { http } = require("../../util/http")

Page({
  data: {
    stocks: [],
    stockIndex: null,
    auto_focus_placeholder: '请输入名称',
    name: '',
    originStores: [],
    stores: []
  },
  onLoad() {
    this.setData({
      stocks: []
    })
    http(storeListUrl, 'GET').then(res => {
      console.log(res)
      this.setData({
        originStores: res,
        stores: res.map(r =>r.name)
      })
    })
  },
  bindPickerChange: function (e) {
		console.log('picker send value:', e.detail.value)
		this.setData({
			stockIndex: e.detail.value
		})
    setTimeout(() =>{
      this.searchStocks()
    })
    
	},
  handleInputBlur(e) {
    const name = e.detail.value;
    this.setData({
      name
    })
    setTimeout(() =>{
      this.searchStocks()
    })
  },
  searchStocks() {
    if (this.data.stockIndex == null) {
      tt.showToast({
        title: '请选择门店',
        duration: 1000,
        icon: 'error',
        mask: false,
      })
      return;
    }
    console.log(this.data.originStores, this.data.stockIndex)
    const store = this.data.originStores[this.data.stockIndex];
    http(stockListUrl, 'GET', { name: this.data.name, storeId: store.id, departmentId: store.feishu_department_id  }).then(res => {
      this.setData({
        stocks: res
      })
    })
  }
})